package jlb.andriva;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import jlb.andriva.wrappers.ClosureExpiration;
import jlb.andriva.wrappers.HttpTask;
import jlb.andriva.wrappers.Util;


public class HomeActivity extends Fragment {

    private View rootView;
    private Element rootElem = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_home, container, false);

        String data = getArguments().getString("data");
        Document doc = org.jsoup.Jsoup.parse(data);
        Element table = doc.select("#plasticId").first();
        if(table != null){
            TextView txt = (TextView) rootView.findViewById(R.id.home_data);
            Element td = table.select("td[align=left]").first();
            String client = td.childNode(1).childNode(0).toString().trim();
            String tc = td.childNode(3).toString().trim();
            String tctype = td.childNode(5).toString().trim();
            String vto = td.childNode(7).toString().trim().replace("&nbsp;", " ");
            txt.setText(client + "\n" + tc + "\n" + tctype + "\n" + vto);
        }

        if(rootElem == null){
        	if(LoginActivity.mTestMode == false){
	            HomeTask mHomeTask = new HomeTask();
	            mHomeTask.execute(getString(R.string.home_url));
        	}
        	else{
        		doc = org.jsoup.Jsoup.parse(Util.getRawContent(rootView.getContext(), R.raw.home));
                rootElem = doc.select("table[border=0]").first();
                if(rootElem != null){
                   paintResults();
                }
        	}
        }
        else{
            //Paint cache results
            paintResults();
        }

        return rootView;
    }

     private class HomeTask extends HttpTask {
         @Override
         protected void onPostExecute(final String result) {
             if (result != null) {
                 Document doc = org.jsoup.Jsoup.parse(result);
                 rootElem = doc.select("table[border=0]").first();
                 if(rootElem != null){
                    paintResults();
                 }
             }
         }
     }

    private void paintResults(){
        ViewGenerator vg = new ViewGenerator();
        vg.execute(rootView.getContext());
        
        /*ClosureExpiration cexp = new ClosureExpiration(rootElem);
        LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.home_exp);
        ll.addView(cexp.getView(rootView.getContext()));*/
    }

    private class ViewGenerator extends AsyncTask <Context, Void, LinearLayout>{

        @Override
        protected LinearLayout doInBackground(Context... contexts) {
            ClosureExpiration cexp = new ClosureExpiration(rootElem);
            return cexp.getView(contexts[0]);
        }

        @Override
        protected void onPostExecute(final LinearLayout result) {
            LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.home_exp);
            ll.addView(result);
        }
    }
}